package com.mantru.iotpot.modbus;

import android.content.Context;
import android.os.Bundle;
import android.os.Message;
import android.webkit.JavascriptInterface;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.text.*;
import java.util.*;
import com.mantru.iotpot.modbus.Common;

public class DownFileThread extends Thread
{
	private Context context;
	private String http_url = "";
	private DownFileObject down_obj;
	public DownFileThread(Context con, String url, DownFileObject obj, boolean is_sync)
	{
		context = con;
		http_url = url.trim();
		down_obj = obj;
		/*
		SaveLog("DownFileThread开始下载,is_sync="+is_sync+",http_url-->" + http_url);
		//is_sync=true表示用多线程下载图片
		if (false == is_sync) {
			long time1 = Common.GetTimeStamp13();
			boolean result1 = Common.SaveUrlToFile(context, http_url);
			if (down_obj != null)
				down_obj.is_down = result1;
			long time2 = Common.GetTimeStamp13();
			SaveLog("DownFileThread下载结果is_sync="+ is_sync +",result=" + result1 + ",use_time="+(time2-time1)+",http_url-->" + http_url);
		}
		*/
	}

	@Override
	public void run()
	{
		try
		{
			long time1 = Common.GetTimeStamp13();
			boolean result1 = Common.SaveUrlToFile(context, http_url);
			if (down_obj != null)
				down_obj.is_down = result1;
			if (result1 == false)
			{
				Thread.sleep(100);
				result1 = Common.SaveUrlToFile(context, http_url);
				if (down_obj != null)
					down_obj.is_down = result1;
			}
			long time2 = Common.GetTimeStamp13();
			SaveLog("DownFileThread下载结果result=" + result1 + ",use_time="+(time2-time1)+",http_url-->" + http_url);
		}
		catch(Exception e)
		{
			Log("DownFileThread Run Error:" + e.getMessage());
		}
		Log("DownFileThread Run End");
		// this.stop();
	}

	public void Log(String str)
	{
		Date nowDate = new Date();
		SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String log_str = "[" + format.format(nowDate) + "] " + str;

		System.out.println(log_str);
	}

	public void SaveLog(String value)
	{
		String api_url = "?txt_type=/pot/save_log/&device_id=image&code=200&timestamp=" + Common.GetTimeStamp13() + "&data=" + URLEncoder.encode(value);
		HttpApi_HttpGet(api_url);
	}

	private int HttpApi_HttpGet(String short_url)
	{
		String http_long_url = Common.API_Server_Url;
		if (short_url.trim().startsWith("?") == false)
			http_long_url += "?";
		if (short_url.trim().toLowerCase().startsWith("http") == false)
			http_long_url += short_url;

		String return_json = "";
		int error_code = 0;      //0表示没有发生异异，1表示有异常
		try
		{
			URL url = new URL(http_long_url);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setRequestMethod("GET");
			InputStream is = conn.getInputStream();
			BufferedReader reader = new BufferedReader(new InputStreamReader(is));
			StringBuilder builder = new StringBuilder();
			String line = "";
			while ((line = reader.readLine()) != null) {
				builder.append(line);
			}
			return_json = builder.toString();
			is.close();
		}
		catch(Exception ex)
		{
			error_code = 1;
			return_json = "error=" + ex.getMessage();
		}

		return 0;
	}



}
